<template>
  <div class="login-container">
      <!--
      el-form 表单组件
      每个表单项都必须使用 el-form-item 组件包裹
     -->

    <div class="login-form-wrap">
      <div class="login-head">
        <div class="logo"></div>
      </div>
      <el-form :model="ruleForm" :rules="rules" class="login-form" ref="form">
        <el-form-item prop="mobile">
          <el-input
            v-model="ruleForm.mobile"
            placeholder="请输入手机号"
            minlength = 11
            maxlength = 11
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="ruleForm.code"
            placeholder="请输入验证码"
            minlength = 6
            maxlength = 6
          ></el-input>
        </el-form-item>
        <el-form-item prop="agree">
          <el-checkbox v-model="ruleForm.agree">我已阅读并同意用户协议和隐私条款</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button
            class="login-btn"
            type="primary"
            :loading="loginLoading"
            @click="onLogin('form')"
          >登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>

// 导入登录api模块
import { login } from '@/api/user'

export default {
  name: 'Login',
  data () {
    return {
      ruleForm: {
        mobile: '13911111111', // 手机号
        code: '246810', // 验证码
        agree: true// 用户协议
      },
      // 用户信息
      userData: [],
      checked: false, // 是否同意协议的选中状态
      loginLoading: false, // 登录的 loading 状态
      rules: { // 表单验证规则
        mobile: [
          // 可以加多条规则
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { pattern: /^1[3|5|7|8|9]\d{9}$/, message: '请输入正确的手机号码' }
        ],
        code: [
          { required: true, message: '请输入手机验证码', trigger: 'blur' },
          { pattern: /^[0-9]{6}$/, message: '请输入正确的验证码' }
        ],
        agree: [
          { validator: (rule, value, callback) => { if (value) { callback() } else { callback(new Error('请同意用户协议')) } }, trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    onLogin (formName) {
      // 表单验证
      this.$refs[formName].validate(async (valid) => {
        // 验证通过
        if (valid) {
          const user = this.ruleForm
          this.loginLoading = true
          const res = await login(user)
          try {
            // 登录成功
            this.$message({
              message: '登录成功',
              type: 'success'
            })

            // 关闭 loading
            this.loginLoading = false

            // 保存token至本地存储

            this.userData = res.data.data

            window.localStorage.setItem('User', JSON.stringify(this.userData))
            // 跳转首页
            this.$router.push({
              name: 'home'
            })
          } catch (err) {
            this.$message.error('登录失败，手机号或验证码错误')
            // 关闭 loading
            this.loginLoading = false
            return Promise.reject(err)
          }
        } else {
          // 验证失败
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: url("./login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("./logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
